﻿using Aliphant.Datas;
using Aliphant.IoC;
using Aliphant.Mvvm.XEvent.Ioc;
using Aliphant.Mvvm.XEvent.UI;
using Aliphant.UI.Browser.Managers;
using Aliphant.UI.Browser.Views;
using Aliphant.UI.Services;

using System.Collections.ObjectModel;
using System.Windows.Input;

namespace Aliphant.UI.Browser.ViewModels
{
    public class BrowserTabViewModel : BindableBase
    {
        public BrowserTabViewModel()
        {
            TabManager.Instance.AddTabEvent += OnAddTab;
            TabManager.Instance.InsertTabEvent += OnInsertTab;
            TabManager.Instance.RemoveTabEvent += OnRemoveTab;

            // 默认打开一个新标签页
            TabManager.Instance.Init();
        }

        private void OnInsertTab(WebView webview, int index)
        {
            TabCollection.Insert(index, webview);
            Selected = index;
        }

        private void OnAddTab(WebView webview)
        {
            TabCollection.Add(webview);
            Selected = TabCollection.IndexOf(webview);
        }

        private void OnRemoveTab(WebView webview)
        {
            TabCollection.Remove(webview);
            if (Selected > 1)
            {
                Selected--;
            }
        }

        private int _selected = -1;
        public int Selected { get => _selected; set => SetProperty(ref _selected, value, UpdateSelected); }

        private void UpdateSelected()
        {
            TabManager.Instance.Selected = _selected;

            if (_selected >= 0 && TabCollection.Count > _selected)
            {
                TabManager.Instance.UpdateInputUrl(TabCollection[_selected].webView.CoreWebView2?.Source ?? string.Empty);
            }
        }

        public ObservableCollection<WebView> TabCollection { get; private set; } = new ObservableCollection<WebView>();

        public void OnNavigateTo(NavigateParameter navigateParameter)
        {
            Init();
            Selected = TabManager.Instance.Selected;
        }

        private void Init()
        {
            TabCollection.Clear();
            var webviews = TabManager.Instance.GetAll();
            foreach (var webview in webviews)
            {
                TabCollection.Add(webview);
            }
        }

        /// <summary>
        /// 新标签页
        /// </summary>
        public ICommand AddTabCommand
        {
            get => new UICommand<object>(obj =>
            {
                TabManager.Instance.Init();
            });
        }

        /// <summary>
        /// 网页收藏
        /// </summary>
        public ICommand CollectionCommand
        {
            get => new UICommand<object>(obj =>
            {
                if (Selected >= 0 && TabCollection.Count > Selected)
                {
                    var webview = TabCollection[Selected];

                    if (webview != null)
                    {
                        // TODO: 收藏网页

                        //var models = ConfigContainer.Instance.cacheManager.Get<List<NavigateWebModel>>("apps");
                        //if (models != null)
                        //{
                        //    var source = webview.webView.CoreWebView2.Source;
                        //    models.Add(new NavigateWebModel
                        //    {
                        //        Name = webview.WebTitle,
                        //        ViewName = "webview",
                        //        WebUri = source
                        //    });
                        //    MessageBox.Show("收藏成功");
                        //}
                        //ConfigContainer.Instance.cacheManager.Update("apps", models);
                    }

                }
            });
        }

    }
}
